package com.example.sqlite.activity;

import net.sf.json.JSONObject;

import com.example.sqlite.R;
import com.example.sqlite.bean.StuBean;
import com.example.sqlite.util.StuDBHelper;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
	
	private Button createBtn;  
	private Button insertBtn;  
	private Button updateBtn;  
	private Button queryBtn;  
	private Button deleteBtn;  
	private Button modifyBtn;  
	private StuDBHelper dbHelper = new StuDBHelper(MainActivity.this, "db_stu", null, 1);
	private SQLiteDatabase database;
	private final static String ERRTAG = "MainActivityErr";
	private final static String TAG = "MainActivity"; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		createBtn = (Button) findViewById(R.id.createBtn);
		insertBtn = (Button) findViewById(R.id.insertBtn);
		updateBtn = (Button) findViewById(R.id.updateBtn);
		queryBtn = (Button) findViewById(R.id.queryBtn);
		deleteBtn = (Button) findViewById(R.id.deleteBtn);
		modifyBtn = (Button) findViewById(R.id.modifyBtn);
		setListener();
	}
	
	private void setListener(){
		createBtn.setOnClickListener(new CreateListener());
		insertBtn.setOnClickListener(new InsertListener());
		updateBtn.setOnClickListener(new UpdateListener());
		queryBtn.setOnClickListener(new QueryListener());
		deleteBtn.setOnClickListener(new DeleteListener());
		modifyBtn.setOnClickListener(new ModifyListener());
	}
	
	class CreateListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			try {
				database = dbHelper.getReadableDatabase();
			} catch (Exception e) {
				Log.e(ERRTAG, "MainActivity::CreateListener异常--->"+e.getMessage());
			}
		}
		
	}
	
	class InsertListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			try {
				database = dbHelper.getWritableDatabase();
				ContentValues values = new ContentValues();
				values.put("id", 1);
				values.put("sname", "test");
				values.put("sage", 18);
				values.put("ssex", "male");
				database.insert("db_stu", null, values);
				database.close();
			} catch (Exception e) {
				Log.e(ERRTAG, "MainActivity::InsertListener异常--->"+e.getMessage());
			}
		}
	}
	
	class  UpdateListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			try {
				dbHelper = new StuDBHelper(MainActivity.this, "db_stu", null, 2);
				database = dbHelper.getReadableDatabase();
			} catch (Exception e) {
				Log.e(ERRTAG, "MainActivity::UpdateListener异常--->"+e.getMessage());
			}
		}
		
	}
	class QueryListener implements OnClickListener{
		
		@Override
		public void onClick(View v) {
			try {
				database = dbHelper.getReadableDatabase();
				Cursor mCursor = database.query("db_stu", new String[]{"id","sname","sage","ssex"}, "id=?", new String[]{"1"}, null, null, null, null);
				StuBean stuBean = new StuBean();
				while(mCursor.moveToNext()){
					int id = mCursor.getInt(mCursor.getColumnIndex("id"));
					String sname = mCursor.getString(mCursor.getColumnIndex("sname"));
					int sage = mCursor.getInt(mCursor.getColumnIndex("sage"));
					String ssex = mCursor.getString(mCursor.getColumnIndex("ssex"));
					stuBean.setId(id);
					stuBean.setSage(sage);
					stuBean.setSname(sname);
					stuBean.setSsex(ssex);
				}
				database.close();
				Log.d(TAG, JSONObject.fromObject(stuBean).toString());
			} catch (Exception e) {
				Log.e(ERRTAG, "MainActivity::QueryListener异常--->"+e.getMessage());
			}
		}
		
	}
	class DeleteListener implements OnClickListener{
		
		@Override
		public void onClick(View v) {
			try {
				database = dbHelper.getWritableDatabase();
				String whereClause = "id=?";
				String whereArgs[] = { "1" };
				int count = database.delete("db_stu", whereClause, whereArgs);
				Log.d(TAG, "删除了"+count+"行");
			} catch (Exception e) {
				Log.e(ERRTAG, "MainActivity::DeleteListener异常--->"+e.getMessage());
			}
		}
		
	}
	class ModifyListener implements OnClickListener{
		
		@Override
		public void onClick(View v) {
			try {
				database = dbHelper.getWritableDatabase();
				ContentValues values = new ContentValues();
				values.put("name", "testOK");
				String whereClause = "id=?";
				String whereArgs[] = {"1"};
				int count = database.update("db_stu", values, whereClause, whereArgs);
				Log.d(TAG, "修改了"+count+"行");
			} catch (Exception e) {
				Log.e(ERRTAG, "MainActivity::ModifyListener异常--->"+e.getMessage());
			}
			
		}
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
